set more off
capture cd "C:\Users\Benjamin Allen\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "~\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"

use "data/Public+Policy+Forum+automation+and+AI+survey+April+2019_June+18,+2019_12.33 cleaned and weighted alg-sup-gov version with scales.dta", clear

********************************************************************************
* Models
********************************************************************************

*------------------------------------------------------------------------------*
* Summary of model variables
*------------------------------------------------------------------------------*

sum alg_govt_acceptable_scale_bi ///
	alg_govt_acc_eff_scale_bi ///
	alg_govt_acc_fair_scale_bi ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale

sum alg_govt_acceptable_scale_ord ///
	alg_govt_acc_eff_scale_ord ///
	alg_govt_acc_fair_scale_ord

*------------------------------------------------------------------------------*
* Run main model
*------------------------------------------------------------------------------*

* Binary version
regress alg_govt_acceptable_scale_bi ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale ///
	[pweight = quota_weight]

estimates store ols_alg_govt_acc_scale_bi

* Ordered version
regress alg_govt_acceptable_scale_ord ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale ///
	[pweight = quota_weight]

estimates store ols_alg_govt_acc_scale_ord

*------------------------------------------------------------------------------*
* Models of sub-scales
*------------------------------------------------------------------------------*

* Just efficiency items

* Binary version
regress alg_govt_acc_eff_scale_bi ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale ///
	[pweight = quota_weight]

estimates store ols_acc_eff_scale_bi

*Ordered version
regress alg_govt_acc_eff_scale_ord ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale ///
	[pweight = quota_weight]

estimates store ols_acc_eff_scale_ord
	
* Just fairness items

* Binary version
regress alg_govt_acc_fair_scale_bi ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale ///
	[pweight = quota_weight]
	
estimates store ols_acc_fair_scale_bi

* Ordered version
regress alg_govt_acc_fair_scale_ord ///
	age_in_years_rescaled ///
	female ///
	income_rescaled ///
	education_rescaled ///
	lr_scale_rescaled ///
	populism_scale ///
	[pweight = quota_weight]
	
estimates store ols_acc_fair_scale_ord

********************************************************************************
* Coefficient plots
********************************************************************************

* Binary version
coefplot ///
	(ols_alg_govt_acc_scale_bi, label("Model 1: Full scale") 					mcolor(edkblue) msymbol(circle) 	msize(vsmall) ciopts(lcolor(edkblue) lwidth(thin))) ///
	(ols_acc_eff_scale_bi, 		label("Model 2: Efficiency") 	mcolor(eltblue) msymbol(triangle) 	msize(vsmall) ciopts(lcolor(eltblue) lwidth(thin))) ///
	(ols_acc_fair_scale_bi, 	label("Model 3: Fairness") 	mcolor(eltgreen) msymbol(square) 	msize(vsmall) ciopts(lcolor(eltgreen) lwidth(thin))) ///
	, drop(_cons) xline(0) ci(95) ///
	xlabels(-.3(.1).3, labsize(vsmall)) ///
	coeflabels( ///
		/* job_instructions_binary = "Follow instructions" */ ///
	, labsize(vsmall) wrap(22)) ///
	title("OLS regressions of support for algorithmic governance""Canada", size(medsmall)) ///
	subtitle("Binary DV", size(small)) ///
	legend(col(1)) ///
	note("Weighted by province, age group, and gender", size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
	
graph export "analysis/graphs/Canada/coefplots/coefplot-ols_alg_govt_acc_scale_bi Canada.png", width(3300) height(2000) replace


* Ordered version
coefplot ///
	(ols_alg_govt_acc_scale_ord, label("Model 1: Full scale") 					mcolor(edkblue) msymbol(circle) 	msize(vsmall) ciopts(lcolor(edkblue) lwidth(thin))) ///
	(ols_acc_eff_scale_ord, 	 label("Model 2: Efficiency") mcolor(eltblue) msymbol(triangle) 	msize(vsmall) ciopts(lcolor(eltblue) lwidth(thin))) ///
	(ols_acc_fair_scale_ord, 	 label("Model 3: Fairness") 	mcolor(eltgreen) msymbol(square) 	msize(vsmall) ciopts(lcolor(eltgreen) lwidth(thin))) ///
	, drop(_cons) xline(0) ci(95) ///
	xlabels(-.3(.1).3, labsize(vsmall)) ///
	coeflabels( ///
		/* job_instructions_binary = "Follow instructions" */ ///
	, labsize(vsmall) wrap(22)) ///
	title("OLS regressions of support for algorithmic governance""Canada", size(medsmall)) ///
	subtitle("Ordered DV", size(small)) ///
	legend(col(1)) ///
	note("Weighted by province, age group, and gender", size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
	
graph export "analysis/graphs/Canada/coefplots/coefplot-ols_alg_govt_acc_scale_ord Canada.png", width(3300) height(2000) replace




********************************************************************************
* Create a table for the models
********************************************************************************

*---------------------------------------------------------------*
* Standalone tables
*---------------------------------------------------------------*

esttab ols_alg_govt_acc_scale_bi ols_acc_eff_scale_bi ols_acc_fair_scale_bi using "analysis/tables/Canada/standalone/ols_alg_govt_acc_scale_bi Canada.tex", replace ///
    b(2) se(2) /// values presented in the table
	nonumbers  noomitted ///
	nodepvars nobase ///
	label wrap ///
	eqlabels(none) ///
	/// eform /// this turns the coefficients into odds ratios
	title("OLS regressions of support for algorithmic governance, Canada") ///
	mtitles("Model 1: Full scale" "Model 2: Efficiency" "Model 3: Fairness") ///
	/// LaTeX options to make the tables look better
	booktabs ///
	width(1.2\hsize) ///
	page(dcolumn) /// Not needed if this is to be called by another tex file
	alignment(D{.}{.}{-1}) /// This aligns the columns on the decimals, but Beamer doesn't like it
	addnotes("Weighted by province, age group, and gender") ///

*---------------------------------------------------------------*
* Tables for use in articles
*---------------------------------------------------------------*

esttab ols_alg_govt_acc_scale_bi ols_acc_eff_scale_bi ols_acc_fair_scale_bi using "analysis/tables/Canada/article/ols_alg_govt_acc_scale_bi Canada.tex", replace ///
    b(2) se(2) /// values presented in the table
	nonumbers  noomitted ///
	nodepvars nobase ///
	label wrap ///
	eqlabels(none) ///
	/// eform /// this turns the coefficients into odds ratios
	title("OLS regressions of support for algorithmic governance, Canada") ///
	mtitles("Model 1: Full scale" "Model 2: Efficiency" "Model 3: Fairness") ///
	/// LaTeX options to make the tables look better
	booktabs ///
	width(1.2\hsize) ///
	///page(dcolumn) /// Not needed if this is to be called by another tex file
	alignment(D{.}{.}{-1}) /// This aligns the columns on the decimals, but Beamer doesn't like it
	addnotes("Weighted by province, age group, and gender") ///

*---------------------------------------------------------------*
* Tables for use in presentations
*---------------------------------------------------------------*

esttab ols_alg_govt_acc_scale_bi ols_acc_eff_scale_bi ols_acc_fair_scale_bi using "analysis/tables/Canada/beamer/ols_alg_govt_acc_scale_bi Canada.tex", replace ///
    b(2) se(2) /// values presented in the table
	nonumbers  noomitted ///
	nodepvars nobase ///
	label wrap ///
	eqlabels(none) ///
	/// eform /// this turns the coefficients into odds ratios
	title("OLS regressions of support for algorithmic governance, Canada") ///
	mtitles("Model 1: Full scale" "Model 2: Efficiency" "Model 3: Fairness") ///
/// LaTeX options to make the tables look better
	booktabs ///
	width(1.2\hsize) ///
	///page(dcolumn) /// Not needed if this is to be called by another tex file
	///alignment(D{.}{.}{-1}) /// This aligns the columns on the decimals, but Beamer doesn't like it
	addnotes("Weighted by province, age group, and gender") ///
	/// Rescale the table so that it fits into a beamer frame
	/// Unfortunately this has to be after the caption for it to work properly, or I could just do this in the presentation file instead
	substitute( ///
	\begin{tabular*} \scalebox{0.65}{\begin{tabular*} ///
	\end{tabular*} \end{tabular*}} ///
	)
	
*---------------------------------------------------------------*
* RTF tables
*---------------------------------------------------------------*

esttab ols_alg_govt_acc_scale_bi ols_acc_eff_scale_bi ols_acc_fair_scale_bi using "analysis/tables/Canada/rtf/ols_alg_govt_acc_scale_bi Canada.rtf", replace ///
    b(2) se(2) /// values presented in the table
	nonumbers  noomitted ///
	nodepvars nobase ///
	label wrap ///
	eqlabels(none) ///
	/// eform /// this turns the coefficients into odds ratios
	title("OLS regressions of support for algorithmic governance, Canada") ///
	mtitles("Model 1: Full scale" "Model 2: Efficiency" "Model 3: Fairness") ///
	/// LaTeX options to make the tables look better
	///booktabs ///
	width(1.2\hsize) ///
	///page(dcolumn) /// Not needed if this is to be called by another tex file
	///alignment(D{.}{.}{-1}) /// This aligns the columns on the decimals, but Beamer doesn't like it
	addnotes("Weighted by province, age group, and gender")
